package com.speech.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.MyApplication;
import com.speech.R;
import com.speech.entity.BaseEntity;
import com.speech.http.BaseCallback;
import com.speech.net.BasicCallBack;
//import com.speech.net.HttpCommunicationManager;
import com.speech.net.constant.EnumReqestActionCode;
import com.speech.net.entity.MessageC2S;
import com.speech.net.entity.MessageC2SBuilder;
import com.speech.net.entity.MessageS2C;
import com.speech.result.Result;
import com.speech.utils.JsonProcessor;

import retrofit2.Call;

public class FindPasswordStep1Activity extends BaseActivity {

    private EditText edt_account;
    private EditText edt_verification;

    private TextView tv_request;

    private Button btn_next;

    private Handler mHandler;

    private final int TAG_TIME_COUNTING = 100;
    private final int TAG_TIME_DONE = 101;

    private boolean bGetCaptcha = false;

    private SweetAlertDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password_step1);
        _initView();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (msg.what == TAG_TIME_COUNTING) {
                    tv_request.setText(msg.arg1 + "s后重发");
                } else if (msg.what == TAG_TIME_DONE) {
                    tv_request.setText("获取验证码");
                    _check_request_able();
                }
            }
        };

        edt_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                _check_request_able();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        edt_verification.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (edt_verification.getText() != null && edt_verification.getText().toString().length() > 0) {
                    btn_next.setClickable(true);
                    btn_next.setTextColor(Color.WHITE);
                } else {
                    btn_next.setClickable(false);
                    btn_next.setTextColor(getResources().getColor(R.color.colorGray_DefaultState));
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //发送短信验证码请求
        tv_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tv_request.setClickable(false);
                tv_request.setTextColor(getResources().getColor(R.color.colorGray_DefaultState));

                tv_request.setText("60s后重发");
                Toast.makeText(FindPasswordStep1Activity.this, "语言验证码正在发送", Toast.LENGTH_LONG).show();



                //TODO: 接口请求 发送验证码
//                MessageC2S msg_c2s = MessageC2SBuilder.buildGeneral_MessageC2S(FindPasswordStep1Activity.this, EnumReqestActionCode.申请验证码, "{\"phone\":" + edt_account.getText().toString() + "}");
//                String json = JsonProcessor.StcGetJson(msg_c2s);
//                HttpCommunicationManager.getInstance().doPost(json, new BasicCallBack(FindPasswordStep1Activity.this, json, new BasicCallBack.BasicCallBackListener() {
//                    @Override
//                    public void success(MessageS2C msg_s2c) {
//                        bGetCaptcha = true;
//                    }
//
//                    @Override
//                    public void fail(MessageS2C msg_s2c) {
//                        new SweetAlertDialog(FindPasswordStep1Activity.this, SweetAlertDialog.ERROR_TYPE)
//                                .setTitleText("错误")
//                                .setContentText(msg_s2c.strMsgUI)
//                                .setConfirmText("OK")
//                                .show();
//                    }
//                }));

                Call<Result<BaseEntity>> call = MyApplication.getInstance()
                        .getApiService().sendCode(edt_account.getText().toString());
                call.enqueue(new BaseCallback<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity baseEntity) {
                        bGetCaptcha = true;
                        Toast.makeText(FindPasswordStep1Activity.this,"发送成功！",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAbnormal(String msg) {
                        Toast.makeText(FindPasswordStep1Activity.this,msg,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(String msg) {
                        dialog = new SweetAlertDialog(FindPasswordStep1Activity.this, SweetAlertDialog.ERROR_TYPE)
                                .setTitleText("错误")
                                .setContentText(msg)
                                .setConfirmText("OK");
                                dialog.show();
                    }
                });

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int iCountDown = 60;
                        while (iCountDown > 0) {
                            Message msg = new Message();
                            msg.arg1 = iCountDown;
                            msg.what = TAG_TIME_COUNTING;
                            mHandler.sendMessage(msg);
                            iCountDown--;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        mHandler.sendEmptyMessage(TAG_TIME_DONE);
                    }
                }).start();
            }
        });


        //下一步
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bGetCaptcha) {
                    if (edt_verification.getText() != null && edt_verification.getText().toString().length() > 0) {
                        String strCaptcha = edt_verification.getText().toString();

                        Intent intent = new Intent(FindPasswordStep1Activity.this, FindPasswordStep2Activity.class);
                        intent.putExtra("strCaptcha", strCaptcha);
                        intent.putExtra("strPhone", edt_account.getText().toString());
                        startActivity(intent);
                        finish();

                    }
                }
            }
        });

    }

    private void _initView() {
        edt_account = findViewById(R.id.edt_find_psw_step1_account);
        edt_verification = findViewById(R.id.edt_find_psw_step1_verification);

        tv_request = findViewById(R.id.tv_find_psw_step1_request);

        btn_next = findViewById(R.id.btn_find_psw_step1_next);
    }

    private void _check_request_able() {
        if (edt_account.getText() != null && edt_account.getText().toString().length() == 11) {
            tv_request.setClickable(true);
            tv_request.setTextColor(getResources().getColor(R.color.colorOrange));
        } else {
            tv_request.setClickable(false);
            tv_request.setTextColor(getResources().getColor(R.color.colorGray_DefaultState));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null){
            dialog.dismissWithAnimation();
        }
    }
}
